package com.mvcMovie.service.impl;

import com.mvcMovie.bean.User;
import com.mvcMovie.mapper.UserMapper;
import com.mvcMovie.service.LoginAndExitService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class LoginAndExitImpl implements LoginAndExitService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public void CookieJudge(HttpServletRequest request, HttpServletResponse response) {
        //获取浏览器的cookie
        Cookie[] cookies = request.getCookies();

        User user;

        String username = null;
        String password = null;

        if (cookies != null){
            for(Cookie cookie : cookies){
                String name = cookie.getName();
                if ("username".equals(name)){
                    //找到用户账号对应的数据
                    username = cookie.getValue();
                }else if("password".equals(name)){
                    //找到用户密码对应的数据
                    password = cookie.getValue();
                }
            }
        }
        user = Login(username, password);
        request.getSession().setAttribute("user",user);
    }

    @Override
    public User Login(String username,String password, String d) {
        String jump = "1".equals(d) ? "root" : "user";
        User user;
        if (jump.equals("user")) {
            user = userMapper.selectLoginByUser(username, password);
        } else {
            user = userMapper.selectLoginByRoot(username, password);
        }
        return user;
    }

    public User Login(String username,String password) {
        return userMapper.selectLoginByUser(username, password);
    }

    @Override
    public void NoLoginJudgment(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        //将此用户存入session域中，前端显示
        User user = (User)session.getAttribute("user");
        String username = user.getUserName();
        String password = user.getUserPassword();
        //创建Cookie对象存储登录名与密码
        Cookie cookie1 = new Cookie("username",username);
        Cookie cookie2 = new Cookie("password",password);
        //设置Cookie的有效时长
        cookie1.setMaxAge(60 * 60 * 24 * 10);
        cookie2.setMaxAge(60 * 60 * 24 * 10);
        //设置Cookie的路径（只要访问该项目。浏览器就要携带这两个cookie）
        cookie1.setPath(request.getContextPath());
        cookie2.setPath(request.getContextPath());
        //响应cookie给浏览器
        response.addCookie(cookie1);
        response.addCookie(cookie2);
    }

    public void Exit(HttpServletRequest request, HttpServletResponse response){
        //获取当前的session对象，销毁
        HttpSession session = request.getSession(false);
        if (session != null){
            //删除当前session域中的user对象
            session.removeAttribute("user");
            //销毁服务器端的session对象
            session.invalidate();
            //销毁cookie（退出系统将所有的cookie销毁）
            Cookie[] cookies = request.getCookies();
            if (cookies != null){
                for (Cookie cookie :cookies){
                    //设置cookie的有效期为0.代表删除该cookie
                    cookie.setMaxAge(0);
                    //设置cookie的路径与浏览器的一致
                    cookie.setPath(request.getContextPath());
                    //响应cookie给浏览器，浏览器会将之前的cookie覆盖，也就是删除
                    response.addCookie(cookie);
                }
            }
        }
    }
}
